//
//  Background.m
//  MessangerSkeleton
//
//  Created by User-15 on 4/28/14.
//  Copyright (c) 2014 IOTrainingCamp. All rights reserved.
//

#import "Background.h"
#import "cocos2d.h"
#import "CCSpriteBatchNode_Private.h"
#import "Configuration.h"

@interface Background ()

@property CCSprite* skyLayer;
@property CCSprite* grassFrontLayer;
@property CCSprite* grassBackLayer;
@property CCSprite* roadLayer;

@end

@implementation Background

- (instancetype)init
{
    self = [super init];
;
    if (self) {
        self.skyLayer = [CCSprite spriteWithImageNamed: @"sky.jpg"];
        [self.skyLayer setAnchorPoint:CGPointZero];
        [self.skyLayer setPosition: CGPointZero];

        
        [self addChild:self.skyLayer z:-4];
        
        self.grassFrontLayer = [CCSprite spriteWithImageNamed: @"grass1.gif"];
        [self.grassFrontLayer setAnchorPoint:CGPointZero];
        [self.grassFrontLayer setPosition: CGPointZero];
        [self.grassFrontLayer setScale:kTextureScale];
        [self addChild:self.grassFrontLayer z:-2];
        
        self.grassBackLayer = [CCSprite spriteWithImageNamed:@"grass2.gif"];
        [self.grassBackLayer setAnchorPoint:CGPointZero];
        [self.grassBackLayer setPosition: CGPointZero];
        [self.grassBackLayer setScale:kTextureScale];
        [self addChild:self.grassBackLayer z:-3];
        
        self.roadLayer = [CCSprite spriteWithImageNamed:@"road.gif"];
        [self.roadLayer setAnchorPoint:CGPointZero];
        [self.roadLayer setPosition: CGPointZero];
        [self.roadLayer setScale:kTextureScale];
        [self addChild:self.roadLayer z:-1];
    }
    return self;
}

- (void) update:(CCTime)dt andSpeed: (CGPoint) speed{
    [self.roadLayer setPosition:ccp(self.roadLayer.position.x - speed.x*dt, self.roadLayer.position.y - speed.y*dt)];
    [self.grassFrontLayer setPosition:ccp(self.grassFrontLayer.position.x - speed.x*dt*0.8, self.grassFrontLayer.position.y - speed.y*dt*0.8)];
    [self.grassBackLayer setPosition:ccp(self.grassBackLayer.position.x - speed.x*dt*0.65, self.grassBackLayer.position.y - speed.y*dt*0.65)];
    
    // looping background 
    if (self.roadLayer.position.x <= (-self.roadLayer.contentSize.width + kTextureOverlapInPixels)*self.roadLayer.scale)
        [self.roadLayer setPosition: CGPointZero];
    if (self.grassFrontLayer.position.x <= (-self.grassFrontLayer.contentSize.width + kTextureOverlapInPixels)*self.grassFrontLayer.scale)
        [self.grassFrontLayer setPosition: CGPointZero];
    if (self.grassBackLayer.position.x <= (-self.grassBackLayer.contentSize.width + kTextureOverlapInPixels)*self.grassBackLayer.scale)
        [self.grassBackLayer setPosition: CGPointZero];
    
    if (self.roadLayer.position.x > 0)
        [self.roadLayer setPosition: ccp((-self.roadLayer.contentSize.width + kTextureOverlapInPixels)*self.roadLayer.scale,0)];
    if (self.grassFrontLayer.position.x > 0)
        [self.grassFrontLayer setPosition: ccp((-self.grassFrontLayer.contentSize.width + kTextureOverlapInPixels)*self.grassFrontLayer.scale,0)];
    if (self.grassBackLayer.position.x > 0)
        [self.grassBackLayer setPosition: ccp((-self.grassBackLayer.contentSize.width + kTextureOverlapInPixels)*self.grassBackLayer.scale,0)];
}

@end
